package Pre.Work;

import java.util.ArrayList;
import java.util.Collections;
import java.util.OptionalDouble;
import java.util.function.ToIntFunction;
import java.util.stream.Stream;

public class Exam04 {
    //有一个整数集合，求每个元素平方的平均值。
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        Collections.addAll(list, 10, 12, 24, 15, 18, 8, 36, 45);
        OptionalDouble avg = list.stream().mapToDouble(number -> Math.pow(number, 2)).average();
        System.out.println("平均值为：" + avg);

        OptionalDouble average = list.stream().mapToInt(new ToIntFunction<Integer>() {
            @Override
            public int applyAsInt(Integer value) {
                return value * value;
            }
        }).average();
        System.out.println(average);
        double v = average.orElse(0.0);//
        double asDouble = average.getAsDouble(); // 如果值不存在会抛出异常
//        double v = (double) average;
        // reduce((i1, i2) -> i1 + i2).get();
//        System.out.println(i / list.size());
        System.out.println(v);
    }
}

